<template>
  <div class="container bg-color-white">
    <div class="bg-color-white border-t border-b pad-10">
      <div class="flex">
        <div class="flex-item align-c" @click="setSort(0)">
          <span>综合</span>
          <div :class="sortIndex===0?'sort-on':'sort-off'"></div>
        </div>
        <div class="flex-item align-c" @click="setSort(1)">
          <div class="flex">
            <span>收益</span>
            <div class="income-sort">
              <img v-if="fileSpace && listQuery.incomeFlag===''" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow3.png'"/>
              <img v-if="fileSpace && listQuery.incomeFlag===0" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow1.png'"/>
              <img v-if="fileSpace && listQuery.incomeFlag===1" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow2.png'"/>
            </div>
          </div>
          <div :class="sortIndex===1?'sort-on':'sort-off'"></div>
        </div>
        <div class="flex-item align-c" @click="setSort(2)">
          <div class="flex">
            <span>零售价</span>
            <div class="price-sort">
              <img v-if="fileSpace && listQuery.sellPriceFlag===''" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow3.png'"/>
              <img v-if="fileSpace && listQuery.sellPriceFlag===0" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow1.png'"/>
              <img v-if="fileSpace && listQuery.sellPriceFlag===1" mode="widthFix" class="width-11 height-15"
                   :src="fileSpace + '/images/arrow2.png'"/>
            </div>
          </div>
          <div :class="sortIndex===2?'sort-on':'sort-off'"></div>
        </div>
      </div>
    </div>
    <div class="align-c pull-refresh">
      <div class="flex flex-wrap">
        <div v-for="(item, index) in listData" :key="index"
             class="flex-item item" :class="listData.length===1?'border-l':''">
          <div>
            <div @click="$pt.navigateTo({url:'/pages/shop/detail/main?goodsType=30&goodsId=' + item.goodsId})">
              <div class="align-c mar-t-4">
                <img :src="item.mainImage + '?imageView2/1/w/300/h/300'" mode="aspectFill" class="img-item"/>
              </div>
              <div class="pad-l-5 pad-t-5 mar-b-10">
                <div class='item-title'>{{ item.title }}</div>
                <div class='font-red mar-t-2 mar-b-5'>零售价：¥{{ item.sellPrice }}</div>
                <div class='div-income mar-t-2 mar-b-5'>预计收益：¥{{ item.income }}</div>
              </div>
              <div v-if="item.upperShelfType===1" class="align-c" @click="selCategory(item.goodsId, index)"
                   :class="!goodsRemove[index][item.goodsId] ? 'btn-upper' : 'btn-upper-off'" catchtap>
                {{ !goodsRemove[index][item.goodsId] ? '添加到店铺' : '已添加' }}
              </div>
              <div v-if="item.upperShelfType!==1" class="align-c btn-upper-off" catchtap>
                已添加
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="listData.length===0 && isInitDone" class="align-c mar-t-30 pad-30">
        <img v-if="fileSpace" mode="widthFix" class="width-153 height-75 mar-t-30"
             :src="fileSpace + '/images/2-0-kong.png'"/>
        <div class="pad-t-20 font-14 font-gray">空空如也</div>
      </div>
    </div>
    <div class="pad-10"></div>
    <!--添加到店铺-->
    <!-- <upper-category ref="upperCategory" @setGoodsRemove="setGoodsRemove"></upper-category> -->
  </div>
</template>

<script>
  // import UpperCategory from '@/components/UpperCategory'
  import {
    pagingProjectGoods
  } from '@/api/distribution'

  export default {
    // components: { UpperCategory },
    data() {
      return {
        listQuery: {
          memberId: '',
          currentPage: 1,
          size: 20,
          incomeFlag: '',
          sellPriceFlag: ''
        },
        listData: [],
        sortIndex: 0,
        pullLoad: {
          loading: false,
          finished: false
        },
        isInitDone: false,
        goodsRemove: [],
        goodsIndex: 0,
        categoryId: ''
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    onShow() {
      const categoryLabel = this.getCurrentPage().query.categoryLabel
      if (categoryLabel) {
        this.$pt.setNavigationBarTitle({
          title: categoryLabel
        })
      }
      this.initGoodsList()
      // 延迟出现空空如也提示
      setTimeout(() => {
        this.isInitDone = true
      }, 500)
    },
    onPullDownRefresh() {
      this.listQuery.currentPage = 1
      this.pullLoad.finished = false
      this.initGoodsList()
      setTimeout(() => {
        this.$pt.stopPullDownRefresh()
      }, 500)
    },
    onReachBottom() {
      if (!this.pullLoad.finished) {
        this.initGoodsList()
      }
    },
    methods: {
      initGoodsList() {
        this.$utils.showToast({
          title: '数据加载中',
          icon: 'loading'
        })
        this.categoryId = this.getCurrentPage().query.categoryId
        this.listQuery.memberId = this.getCustomerId
        this.listQuery.enterpriseId = this.getEnterpriseId
        this.listQuery.categoryId = this.categoryId
        pagingProjectGoods(this.listQuery).then(response => {
          if (response.isError) return
          this.$pt.hideToast()
          if (this.listQuery.currentPage === 1) {
            this.listData = []
            this.goodsRemove = []
          }
          response.data.records.forEach(item => {
            // 处理字段长度
            item.title = this.$utils.getStrByLength(item.title, 20)
            this.listData.push(item)
            this.goodsRemove.push(item)
          })
          // 翻页+1
          this.listQuery.currentPage++
          // 不使用total，提升后端接口性能
          if (this.listData.length === 0 || this.listQuery.size > response.data.records.length) {
            this.pullLoad.finished = true
          }
        }).catch(() => {
        })
      },
      setSort(sortIndex) {
        this.sortIndex = sortIndex
        switch (this.sortIndex) {
          default:
          case 0:
            this.listQuery.incomeFlag = ''
            this.listQuery.sellPriceFlag = ''
            break
          case 1:
            this.listQuery.incomeFlag = this.listQuery.incomeFlag ? 0 : 1
            this.listQuery.sellPriceFlag = ''
            break
          case 2:
            this.listQuery.incomeFlag = ''
            this.listQuery.sellPriceFlag = this.listQuery.sellPriceFlag ? 0 : 1
            break
        }
        this.listQuery.currentPage = 1
        this.pullLoad.finished = false
        this.initGoodsList()
      },
      selCategory(goodsId, index) {
        if (index || index === 0) {
          this.goodsIndex = index
          if (this.goodsRemove[index][goodsId]) return
        }
        this.$refs.upperCategory.selCategory(goodsId)
      },
      setGoodsRemove(goodsId) {
        // 重新赋值，刷新数据
        this.goodsRemove = this.$utils.deepClone(this.goodsRemove)
        this.goodsRemove[this.goodsIndex][goodsId] = true
      }
    }
  }
</script>

<style scoped>
  .sort-on {
    background-color: #f8c400;
    width: 50px;
    height: 3px;
    border-radius: 2px;
  }

  .sort-off {
    background-color: #fff;
    width: 50px;
    height: 3px;
    border-radius: 2px;
  }

  .income-sort {
    position: absolute;
    margin-top: 5px;
    margin-left: 40px;
  }

  .price-sort {
    position: absolute;
    margin-top: 5px;
    margin-left: 55px;
  }

  .pull-refresh {
    background-color: #fff;
    min-height: calc(100vh - 200px);
  }

  .item {
    max-width: 177px;
    width: 177px;
    background-color: #fff;
    border-right: 0.5px solid #d9d9d9;
    border-bottom: 0.5px solid #d9d9d9;
    padding: 5px;
  }

  .img-item {
    width: 170px;
    height: 170px;
  }

  .item-title {
    height: 25px;
    overflow: hidden;
  }

  .get-category {
    width: 375px;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 990;
  }

  .bg-confirm {
    background-color: #f8c400;
  }

  .btn-upper {
    margin: 5px;
    padding: 2px;
    background-color: #f8c400;
    border-radius: 5px;
  }

  .btn-upper-off {
    margin: 5px;
    padding: 2px;
    background-color: #cccccc;
    border-radius: 5px;
  }

  .upper-all-btn {
    width: 45px;
    height: 45px;
    position: fixed;
    bottom: 100px;
    right: 40px;
    z-index: 100;
    background-color: #ffc400;
    border-radius: 50%;
    font-size: 12px;
    font-weight: bold;
    line-height: 1.2;
  }

  .div-income {
    border-radius: 5px;
    background-color: #fbf2d4;
    font-size: 12px;
    padding-top: 2px;
    padding-left: 10px;
    padding-right: 10px;
    width: 110px;
  }
</style>
